package com.neusoft.service.impl;

import com.neusoft.mapper.PointsMapper;
import com.neusoft.po.Points;
import com.neusoft.service.PointsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PointsServiceImpl implements PointsService {

    @Autowired
    private PointsMapper pointsMapper;

    @Override
    public List<Points> getAvailablePoints(String userId) {
        Date currentDate = new Date();
        return pointsMapper.getAvailablePoints(userId, currentDate);
    }

    @Override
    public List<Points> getExpiredPoints(String userId) {
        Date currentDate = new Date();
        return pointsMapper.getExpiredPoints(userId, currentDate);
    }

    @Override
    public Integer getTotalAvailablePoints(String userId) {
        Date currentDate = new Date();
        return pointsMapper.getTotalAvailablePoints(userId, currentDate);
    }

    @Override
    public void usePoints(String userId, Integer pointsToUse) {
        Date currentDate = new Date();
        pointsMapper.usePoints(userId, pointsToUse, currentDate);
    }

    @Override
    public void addPoints(String userId, Integer pointsToAdd) {
        Date currentDate = new Date();
        pointsMapper.addPoints(userId, pointsToAdd, currentDate);
    }

    @Override
    @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨 0 点执行一次
    public void updateExpiredPoints() {
        Date currentDate = new Date();
        pointsMapper.updateExpiredPoints(currentDate);
    }
}